package com.sisyphus.spinach.network.utils;

import android.text.TextUtils;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import androidx.annotation.StringRes;

/**
 * 表单校验工具
 */
public class Form {

    /**
     * 校验规则
     */
    public interface Rule {

        boolean validation();

        CharSequence getErrorMessage();

    }


    /**
     * 相同
     */
    public static class Same implements Form.Rule {

        private final CharSequence param1, param2;

        private final String errorMessage;

        public Same(TextView param1, TextView param2, @StringRes int errorStringRes) {
            this(param1.getText(), param2.getText(), param1.getContext().getString(errorStringRes));
        }

        public Same(TextView param1, TextView param2, String errorMessage) {
            this(param1.getText(), param2.getText(), errorMessage);
        }

        public Same(CharSequence param1, CharSequence param2, String errorMessage) {
            this.param1 = param1;
            this.param2 = param2;
            this.errorMessage = errorMessage;
        }

        @Override
        public boolean validation() {
            return param1.equals(param2);
        }

        @Override
        public CharSequence getErrorMessage() {
            return errorMessage;
        }

    }


    /**
     * 不能为空
     */
    public static class NotEmpty implements Form.Rule {

        private final String errorMessage;

        private final TextView[] params;

        public NotEmpty(List<TextView> params, String errorMessage) {
            this.params = new ArrayList<>(params).toArray(new TextView[params.size()]);
            this.errorMessage = errorMessage;
        }

        public NotEmpty(List<TextView> params, @StringRes int errorStringRes) {
            this(params, params.get(0).getContext().getResources().getString(errorStringRes));
        }

        public NotEmpty(TextView param, @StringRes int errorStringRes) {
            this(Collections.singletonList(param), errorStringRes);
        }

        public NotEmpty(TextView param, String errorMessage) {
            this(Collections.singletonList(param), errorMessage);
        }

        @Override
        public boolean validation() {
            for (TextView textView : params) {
                if (textView == null
                        || TextUtils.isEmpty(textView.getText())
                        || TextUtils.isEmpty(textView.getText().toString().trim())) {
                    return false;
                }
            }

            return true;
        }

        @Override
        public CharSequence getErrorMessage() {
            return errorMessage == null ? "" : errorMessage;
        }

    }


    /**
     * 长度限定
     */
    public static class Length implements Form.Rule {

        private final int length, min, max;

        private final CharSequence errorMessage;

        public Length(int length, int min, CharSequence errorMessage) {
            this(length, min, Integer.MAX_VALUE, errorMessage);
        }

        public Length(TextView param1, int min, @StringRes int errorStringRes) {
            this(param1.getText().length(), min, Integer.MAX_VALUE, param1.getContext().getString(errorStringRes));
        }

        public Length(TextView param1, int min, CharSequence errorMessage) {
            this(param1.getText().length(), min, Integer.MAX_VALUE, errorMessage);
        }

        public Length(TextView param1, int min, int max, CharSequence errorMessage) {
            this(param1.getText().length(), min, max, errorMessage);
        }

        public Length(int length, int min, int max, CharSequence errorMessage) {
            this.length = length;
            this.min = min;
            this.max = max;
            this.errorMessage = errorMessage;
        }

        @Override
        public boolean validation() {
            return length <= max && length >= min;
        }


        @Override
        public CharSequence getErrorMessage() {
            return errorMessage;
        }
    }

}
